/**
 * 
 */
package dt.inlee.common.mail;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import dt.inlee.common.util.BaseUtil;
import dt.inlee.common.util.SystemResourceUtil;

/**
 * @功能说明：邮件发送服务器
 * @作者： 印鲜刚
 * @创建日期： 2010-9-13
 * @
 */
public class MailSendServer {
	protected static Log log = LogFactory.getLog(MailSendServer.class);
	// 邮件发生参数设置类
	private SendMail sendMail;
	// 抄送人
	private String cc;
	// 秘送人
	private String bcc;
	// 邮件发送目标地址
	private String to;
	// 邮件发送主题
	private String subject;
	// 邮件发送内容
	private String content;
	// 邮件发送附件
	private File file;
	// 邮件发送附件别名
	private String actualFileName;
	// 邮件发送多附件
	private Map<String, File> mapAffixs = new HashMap<String, File>();

	/**
	 * 
	 * @param person
	 *            发送人信息描述
	 */
	public MailSendServer(String person) throws Exception {
		try {

			sendMail = new SendMail(SystemResourceUtil.getInstance().getResourceAsString("SMTP_HOST"));
			sendMail.setNamePass(SystemResourceUtil.getInstance().getResourceAsString("SMTP_USER_NAME"),
					SystemResourceUtil.getInstance().getResourceAsString("SMTP_USER_PASSWORD"));
			String setFrom = SystemResourceUtil.getInstance().getResourceAsString("SMTP_USER_NAME") + "@"
					+ SystemResourceUtil.getInstance().getResourceAsString("SMTP_HOST").substring(5);
			sendMail.setFrom(setFrom, person);

		} catch (Exception e) {
			e.printStackTrace();
			log.info("请检测邮件服务器参数设置是否正确？");
			throw new Exception(e.getMessage());
		}

	}

	public MailSendServer() throws Exception {
		this("共建共享系统");
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getCc() {
		return cc;
	}

	public void setCc(String cc) {
		this.cc = cc;
	}

	public String getBcc() {
		return bcc;
	}

	public void setBcc(String bcc) {
		this.bcc = bcc;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getActualFileName() {
		return actualFileName;
	}

	public void setActualFileName(String actualFileName) {
		this.actualFileName = actualFileName;
	}

	public Map<String, File> getMapAffixs() {
		return mapAffixs;
	}

	public void setMapAffixs(Map<String, File> mapAffixs) {
		this.mapAffixs = mapAffixs;
	}

	/**
	 * 邮件发送
	 * 
	 * @param to
	 * @param subject
	 * @param content
	 * @param file
	 * @param actualFileName
	 * @return
	 * @throws Exception
	 */
	public boolean sendMailOut() throws Exception {
		try {
			if (!BaseUtil.isEmpty(to))
				this.sendMail.setTo(to);
			if (!BaseUtil.isEmpty(cc))
				this.sendMail.setCC(cc);
			if (!BaseUtil.isEmpty(bcc))
				this.sendMail.setBCC(bcc);
			if (!BaseUtil.isEmpty(subject))
				this.sendMail.setSubject(subject);
			if (!BaseUtil.isEmpty(content))
				this.sendMail.setBody(content);
			if (!BaseUtil.isEmpty(file)) {
				if (!!BaseUtil.isEmpty(actualFileName))
					this.sendMail.addFileAffix(file, actualFileName);
				else
					this.sendMail.addFileAffix(file);
			} else {
				Set<String> fileNameSet = this.mapAffixs.keySet();
				for (String tempFileName : fileNameSet) {
					File tempFile = this.mapAffixs.get(tempFileName);
					if (!BaseUtil.isEmpty(tempFile)) {
						this.sendMail.addFileAffix(tempFile, tempFileName);
					}
				}
			}
			if (this.sendMail.sendout()) {
				log.info("发送邮件成功！");
				return true;
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.info("发送邮件失败");
			throw new Exception(e.getMessage());
		}
		return false;
	}
}
